using System;

namespace Common.Database
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class  DbTableKey:IComparable 
    {
        private DataProviderType _DbProvider;
        private string _ConnStr;
        private string _SchemaName;
        private string _TblName;
        private string _Key;

        /// <summary>
        /// 
        /// </summary>
        public DbTableKey()
        {
            this._DbProvider = DataProviderType.MSSQL;
            this._ConnStr = "Server=(local);Database=Master;Integrated Security=true;";
            this._SchemaName = string.Empty;
            this._TblName = "";
            this._Key = "{" + ((int)this.DbProvider).ToString() + "}{" +
                        this.ConnStr + "}{" +
                        (string.IsNullOrEmpty(this.SchemaName) ? this.TblName : this.SchemaName + "." + this.TblName) +
                        "}";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbProviderType"></param>
        /// <param name="connStr"></param>
        /// <param name="schemaName"></param>
        /// <param name="tableName"></param>
        public DbTableKey(DataProviderType dbProviderType, string connStr, string schemaName, string tableName)
        {
            this.DbProvider = dbProviderType;
            this.ConnStr = connStr;
            this.SchemaName = schemaName;
            this.TblName = tableName;
            this.Key = "{" + ((int) this.DbProvider).ToString() + "}{" +
                        this.ConnStr + "}{" +
                        (string.IsNullOrEmpty(this.SchemaName) ? this.TblName : this.SchemaName + "." + this.TblName) +
                        "}";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public DbTableKey(string key)
        {
            int pos1 = key.IndexOf("{");
            string str1 = key.Substring(pos1 + 1, key.IndexOf("}", pos1) - 1);
            this._DbProvider = (DataProviderType) (int.Parse(str1));

            int pos2 = key.IndexOf("{", pos1 + 1);
            string str2 = key.Substring(pos2 + 1, key.IndexOf("}", pos2) - 1);
            this._ConnStr = str2;

            int pos3 = key.IndexOf("{", pos2 + 1);
            string str3 = key.Substring(pos3 + 1, key.IndexOf("}", pos3) - 1);
            if(str3.IndexOf(".")>0)
            {
                this._SchemaName = str3.Substring(0, str3.IndexOf("."));
                this._TblName = str3.Substring(str3.IndexOf(".") + 1);
            }
            else
            {
                this._SchemaName = string.Empty;
                this._TblName = str3;
            }
            this._Key = key;
        }

        #region props
        /// <summary>
        /// 
        /// </summary>
        public DataProviderType DbProvider
        {
            get { return _DbProvider; }
            set { _DbProvider = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string ConnStr
        {
            get { return _ConnStr; }
            set { _ConnStr = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string SchemaName
        {
            get { return _SchemaName; }
            set { _SchemaName = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string TblName
        {
            get { return _TblName; }
            set { _TblName = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Key
        {
            get { return _Key; }
            set { _Key = value; }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            if(obj.GetType()==typeof(DbTableKey))
            {
                DbTableKey otherKey = (DbTableKey) obj;
                return this.Key.ToLower().CompareTo(otherKey.Key.ToLower());
            }
            else
            {
                throw new Exception("Comparable object must be of type DbTableKey");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this.Key.ToLower().GetHashCode();
        }
    }
}
